import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86183
 * Date: 2024-04-11
 * Time: 19:24
 */
public class Demo4 {
    public static void main(String[] args) {
        //自动扩容
//        ExecutorService service = Executors.newCachedThreadPool();
//        for (int i = 0; i < 10000; i++) {
//            int id = i;
//            service.submit(new Runnable() {
//                @Override
//                public void run() {
//                    System.out.println("hello " + id + ",  " + Thread.currentThread().getName());
//                }
//            });
//        }
        //最大线程数
        ExecutorService service1 = Executors.newFixedThreadPool(10);
        for (int i = 0; i < 10000; i++) {
            int id = i;
            service1.submit(new Runnable() {
                @Override
                public void run() {
                    System.out.println("hello " + id + "," + Thread.currentThread().getName());
                }
            });

        }
//        ExecutorService service= Executors.newSingleThreadExecutor();
        //线程池中只有一个线程
//        for (int i = 0; i < 10000; i++) {
//            int id = i;
//           service.submit(new Runnable() {
//             @Override
//              public void run() {
//                  System.out.println("hello " + id + "," + Thread.currentThread().getName());
//               }
//         });
//
//       }

        ExecutorService service=Executors.newScheduledThreadPool(4);
        for (int i = 0; i < 10000; i++) {
            int id = i;
            service.submit(new Runnable() {
                @Override
                public void run() {
                    System.out.println("hello " + id + "," + Thread.currentThread().getName());
                }
            },1000);

        }

    }

}
